	package map.abstractor.xaml2uimodel.base;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;


/**
 *  Abstract Class XMLDocument
 * 	Represent any XML document (MXML, XAML, ...)
 *
 */
public abstract class XMLDocument {
	
	/**
	 * Fields
	 */
	protected Document document;
	protected XMLElement application;
	
	
	/**
	 * Constructor
	 * @param filename : XML file name
	 * @throws ParserConfigurationException 
	 * @throws IOException 
	 * @throws SAXException 
	 */
	public XMLDocument(String filename) throws ParserConfigurationException, SAXException, IOException{
		DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
		DocumentBuilder constructeur = fabrique.newDocumentBuilder();
		constructeur.setErrorHandler(new XMLErrorHandler());
		File mxml = new File(filename);
		document = constructeur.parse(mxml);
		application = null;	
	}
	
	/**
	 * Constructor
	 * @param content : InputStream of content
	 * @throws ParserConfigurationException 
	 * @throws IOException 
	 * @throws SAXException 
	 */
	public XMLDocument(InputStream content) throws ParserConfigurationException, SAXException, IOException{
		DocumentBuilderFactory fabrique = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder constructeur = fabrique.newDocumentBuilder();
		constructeur.setErrorHandler(new XMLErrorHandler());
		document = constructeur.parse(content);
		application = null;
	}
	
	
	/**
	 * process : create objet from DOM/SAX 
	 * @param element : DOM Element
	 * @return XMLObject : the root xml Node
	 */
	protected abstract XMLObject process(Element element);
	
	
	/**
	 * getApplication
	 * @return : the root xml node 
	 */
	public XMLElement getApplication() {
		return application;
	}
	
	public Element getElement(){
		return document.getDocumentElement();
	}
	
}
